package com.tuo51.evaluate.controller;

import com.tuo51.core.domain.staffs.Staff;
import com.tuo51.core.service.StaffService;
import com.tuo51.evaluate.domain.evaluatingV2.EvaluateV2Record;
import com.tuo51.evaluate.repositories.AnswerNativeRepository;
import com.tuo51.evaluate.service.EvaluateV2ConclusionService;
import com.tuo51.evaluate.service.EvaluateV2Service;
import com.tuo51.evaluate.vo.evaluate.EvaluateV2AnswerVO;
import com.tuo51.foudation.exception.BusinessException;
import com.tuo51.foudation.model.ErrorCode;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.utils.CopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import static com.tuo51.foudation.constant.RequestConstant.APP_CHANNEL;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * 测评管理系统
 */

@RestController
@RequestMapping("/eapi/manager")
@Api(value = "/eapi/operator", tags = "测评 - 管理（deprecated）")
public class EvaluatePBManagerController {

    @Autowired
    private EvaluateV2Service evaluateService;

    @Autowired
    private EvaluateV2ConclusionService conclusionService;

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private AnswerNativeRepository answerRepository;

    @Autowired
    private StaffService staffService;

    @ApiOperation(value = "测评报告详情",httpMethod = "POST")
    @RequestMapping("/detail")
    public StatusResponse evaluateDetail(String evaluateRecordId,
                                         @RequestHeader(value = APP_CHANNEL, required = false) String channel) {
        EvaluateV2Record object = evaluateService.getEvaluateDetail(evaluateRecordId, channel);

        Map result = conclusionService.generateReportExtInfo(object);

        List<EvaluateV2AnswerVO> answerSheet = answerRepository.findAnswerSheet(evaluateRecordId);
        result.put("answerSheet", answerSheet);

        result.putAll(CopyUtils.transBean2Map(object));

        return StatusResponse.success(result);
    }

    @RequestMapping(value = "/compare_with", method = POST)
    public StatusResponse compareWithReport(@RequestParam("evaluateRecordId") String evaluateRecordId,
                                            @RequestParam("comparingWithId") String comparingWithId) {

        if(evaluateRecordId.equals(comparingWithId)){
            throw new BusinessException(ErrorCode.EVALUATE_NO_MATCH, "被比较报告不能是当前报告。");
        }

        EvaluateV2Record record = evaluateService.getEvaluateDetail(evaluateRecordId, false);
        EvaluateV2Record comparing = evaluateService.getEvaluateDetail(comparingWithId, false);

        int compare = comparing.getEvaluatingDate().compareTo(record.getEvaluatingDate());
        if(compare > 0){
            EvaluateV2Record r = record;
            record = comparing;
            comparing = r;
        }

        Staff staff = staffService.getById(record.getEvaluatingTeacherId());
        //检查vip状态
        staffService.checkUserVIPStatus(staff);

        Map result = conclusionService.generateReportExtInfo(record);

        result.putAll(CopyUtils.transBean2Map(record));
        result.put("compareWith", comparing);
        result.put("compareDQConclusion", conclusionService.getDQComparingConclusion(record, comparing));
        result.put("compareHeightConclusion", conclusionService.getHeightComparingConclusion(record, comparing));
        result.put("compareWeightConclusion", conclusionService.getWeightComparingConclusion(record, comparing));

        return StatusResponse.success(result);
    }
}
